﻿
using MetroApp.Common;
using MetroApp.Helper;
using MetroApp.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Windows.ApplicationModel;
namespace MetroApp.ViewModels
{
    public class HoneyItemViewModel : NotifyPropertyChangeBase
    {
        #region Fields
        private ObservableCollection<HoneyEntity> _items = new ObservableCollection<HoneyEntity>();
        private double _width = 250;
        private double _containerHeight = 560;
        private double _baseHeight = 280;
        #endregion

        #region Constructor
        public HoneyItemViewModel()
        {
            if (DesignMode.DesignModeEnabled)
            {
                List<HoneyEntity> honeyList = new List<HoneyEntity>();
                honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15866793897_146x219.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrl = "http://img.geilicdn.com/taobao19765708787_146x194.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrl = "http://img.geilicdn.com/taobao15848113020_146x146.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15848325228_146x219.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.03, ImageUrl = "http://img.geilicdn.com/taobao19779004225_146x150.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.57, ImageUrl = "http://img.geilicdn.com/taobao15865634518_146x229.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrl = "http://img.geilicdn.com/taobao15865434962_146x144.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15830113145_146x219.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.15, ImageUrl = "http://img.geilicdn.com/taobao17077543430_146x167.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrl = "http://img.geilicdn.com/taobao19726272617_146x213.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrl = "http://img.geilicdn.com/taobao15865557654_146x163.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrl = "http://img.geilicdn.com/taobao19776424457_146x201.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrl = "http://img.geilicdn.com/taobao19712332041_146x144.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrl = "http://img.geilicdn.com/taobao19722332410_146x146.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15851750545_146x219.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.35, ImageUrl = "http://img.geilicdn.com/taobao15857918167_146x197.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrl = "http://img.geilicdn.com/taobao19704356816_146x223.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrl = "http://img.geilicdn.com/taobao19725572516_146x194.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao19725916030_146x219.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.60, ImageUrl = "http://img.geilicdn.com/taobao15845742605_146x233.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.41, ImageUrl = "http://img.geilicdn.com/taobao15842817755_146x205.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15835866335_146x219.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrl = "http://img.geilicdn.com/taobao15693786979_146x223.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrl = "http://img.geilicdn.com/taobao17072171509_146x194.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 0.67, ImageUrl = "http://img.geilicdn.com/taobao17072151693_146x97.jpg" });
                honeyList.Add(new HoneyEntity { ImgRatio = 1.51, ImageUrl = "http://img.geilicdn.com/taobao15882494639_146x220.jpg" });
                //honeyList.Add(new HoneyEntity { ImgRatio = 1.49, ImageUrl = "http://img.geilicdn.com/taobao19813600740_146x217.jpg" });

                CaculateItems(honeyList);

                honeyList.ToObservableCollection(this.Items);
            }
            
        }

        private void CaculateItems(List<HoneyEntity> honeyList)
        {
            for (int i = 0; i < honeyList.Count; i++)
            {
                if (i % 2 == 1)
                {
                    HoneyEntity lastItem = honeyList[i - 1];
                    HoneyEntity currentItem = honeyList[i];
                    double lastHeight = lastItem.ImgRatio * _width;
                    double currentHeight = currentItem.ImgRatio * _width;
                    lastItem.Width = _width;
                    lastItem.Height =  _containerHeight * (lastHeight / (lastHeight + currentHeight));
                    lastItem.Margin = "0,0,0," + (_baseHeight - lastItem.Height).ToString();
                    Debug.WriteLine("lastMargin :   " + lastItem.Margin);

                    currentItem.Width = _width;
                    currentItem.Height = _containerHeight - lastItem.Height;
                    currentItem.Margin = "0," + (_baseHeight - currentItem.Height).ToString() + ",0,0";
                    Debug.WriteLine("currentMargin :   " + currentItem.Margin);
                    //Debug.WriteLine("last:" + lastItem.ImageUrl + "width:" + lastItem.Width + "height:" + lastItem.Height);
                    //Debug.WriteLine("current:" + currentItem.ImageUrl + "width:" + currentItem.Width + "height:" + currentItem.Height);
                } 
            }
        }
        #endregion
        
        public ObservableCollection<HoneyEntity> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.OnPropertyChanged(() => this.Items);
                }
            }
        }

        internal void Load()
        {
            List<HoneyEntity> honeyList = new List<HoneyEntity>();
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15866793897_146x219.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrl = "http://img.geilicdn.com/taobao19765708787_146x194.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrl = "http://img.geilicdn.com/taobao15848113020_146x146.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15848325228_146x219.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.03, ImageUrl = "http://img.geilicdn.com/taobao19779004225_146x150.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.57, ImageUrl = "http://img.geilicdn.com/taobao15865634518_146x229.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrl = "http://img.geilicdn.com/taobao15865434962_146x144.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15830113145_146x219.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.15, ImageUrl = "http://img.geilicdn.com/taobao17077543430_146x167.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrl = "http://img.geilicdn.com/taobao19726272617_146x213.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrl = "http://img.geilicdn.com/taobao15865557654_146x163.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrl = "http://img.geilicdn.com/taobao19776424457_146x201.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrl = "http://img.geilicdn.com/taobao19712332041_146x144.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrl = "http://img.geilicdn.com/taobao19722332410_146x146.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15851750545_146x219.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.35, ImageUrl = "http://img.geilicdn.com/taobao15857918167_146x197.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrl = "http://img.geilicdn.com/taobao19704356816_146x223.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrl = "http://img.geilicdn.com/taobao19725572516_146x194.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao19725916030_146x219.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.60, ImageUrl = "http://img.geilicdn.com/taobao15845742605_146x233.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.41, ImageUrl = "http://img.geilicdn.com/taobao15842817755_146x205.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrl = "http://img.geilicdn.com/taobao15835866335_146x219.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrl = "http://img.geilicdn.com/taobao15693786979_146x223.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrl = "http://img.geilicdn.com/taobao17072171509_146x194.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.67, ImageUrl = "http://img.geilicdn.com/taobao17072151693_146x97.jpg" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.51, ImageUrl = "http://img.geilicdn.com/taobao15882494639_146x220.jpg" });
            //honeyList.Add(new HoneyEntity { ImgRatio = 1.49, ImageUrl = "http://img.geilicdn.com/taobao19813600740_146x217.jpg" });
            CaculateItems(honeyList);
            honeyList.ToObservableCollection(this.Items);
        }
    }
}
